package com.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pojo.Archive;
import com.pojo.ArchiveFile;
import com.service.ArchiveService;
import com.service.FileService;
import com.service.UlogService;
@Controller
public class ArchiveController {
	ArchiveService service=new ArchiveService();
	FileService service1=new FileService();
	UlogService service2=new UlogService();
	
	@RequestMapping(value="/asasda.action",method=RequestMethod.GET)
	@ResponseBody
	public List<Archive> query(){
		List<Archive> list=service.query();
		return list;
	}
	@RequestMapping(value="/query_file.action",method=RequestMethod.GET)
	@ResponseBody
	public List<ArchiveFile> file_query(
			@RequestParam(value="id",defaultValue="") String id
			){
		List<ArchiveFile> file=service1.file_query();
		return file;
	}
	@RequestMapping(value="/del.action",method=RequestMethod.GET)
	@ResponseBody
	public void del(
			@RequestParam(value="id") String id
			){
		service.del(id);
	}
	@RequestMapping(value="/hero.action",method=RequestMethod.POST)
	@ResponseBody
	public void addarchive(
			@RequestParam(value="archive_id") String archive_id,
			@RequestParam(value="identity") String identity,
			@RequestParam(value="name") String name,
			@RequestParam(value="gender") String gender,
			@RequestParam(value="birthday") String birthday,
			@RequestParam(value="date_retire1") String date_r1,
			@RequestParam(value="date_retire2") String date_r2,
			@RequestParam(value="sp") String sp,
			@RequestParam(value="fc") String fc,
			@RequestParam(value="title") String title,
			@RequestParam(value="seniority") String seniority,
			@CookieValue(value="us") String usId
			){
		Archive arch=service.queryid();
		service2.ulogadd(usId, arch.getId().toString(), "添加档案");
		service.addarchive(archive_id, identity, name, gender, birthday, date_r1, date_r2, sp, fc, title, seniority);
	
	}
	@RequestMapping(value="/queryOne.action",method=RequestMethod.GET)
	@ResponseBody
	public String queryOne(
			@RequestParam(value="archive_id") String archive_id,
		//@RequestParam(value="identity") String identity
			@CookieValue(value="us") String usId
			){
		List<Archive> arc=service.queryOne(archive_id);
		Archive arch=service.queryid();
		service2.ulogadd(usId, arch.getId(), "查看档案");
		//System.out.println(arc.get(0).getId());
		if(arc.size()==0){return "err";}
		return arc.get(0).getId();
	}
	@RequestMapping(value="/One.action",method=RequestMethod.GET)
	@ResponseBody
	public Archive One(
			@RequestParam(value="id",defaultValue="") String id
			){
		Archive arc=service.One(id);
		return arc;
	}
	@RequestMapping(value = "/{id}/upload.action", method = RequestMethod.POST)
    @ResponseBody
    public String upload(
        @PathVariable String id,
        @RequestParam("file") MultipartFile file,
        HttpServletRequest req
    ) {
        if (file.isEmpty()) {
            System.out.println("未选择文件");
            return "ERROR_NO_FILE";
        }
        try {
            byte[] bytes = file.getBytes();
            String fileName = file.getOriginalFilename();
            String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
            String newFileName = String.valueOf(System.currentTimeMillis()) + "." + fileExtension;
            Archive archive = service.One(id);
            String filePath = String.format("%s\\%s\\%s\\%s",
                req.getSession().getServletContext().getRealPath("/"),
                "upload",
                archive.getArchive_id(),
                newFileName);
            File existsFile = new File(filePath);
            if (!existsFile.getParentFile().exists()) {
                if (!existsFile.getParentFile().mkdirs()) {
                    System.out.println("创建目录失败");
                    return "ERROR_ON_MKDIR";
                }
            }
            if (existsFile.exists()) {
                if (existsFile.delete()) {
                  // System.out.println("删除原始文件：" + filePath);
                } else {
                    System.out.println("删除文件失败：" + filePath);
                }
            }
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(
            new File(filePath)));
            stream.write(bytes);
            stream.close();
            service1.file_add(String.valueOf(archive.getArchive_id()), newFileName);
            
            return "OK";
        } catch (Exception e) {
            e.printStackTrace();
            return "ERRO";
       }
	}
	
} 
